from rest_framework import serializers

from management import models


class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = '__all__'


class UserSerializer(serializers.ModelSerializer):
    rid = RoleSerializer()

    class Meta:
        model = models.User
        fields = '__all__'


class UserDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.User
        fields = '__all__'


# class FirstResourceSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = models.Resource
#         fields = '__all__'
#
#
# class ResourceSerializer(serializers.ModelSerializer):
#     son = FirstResourceSerializer(many=True)
#
#     class Meta:
#         model = models.Resource
#         fields = '__all__'


class ResourceSerializer(serializers.ModelSerializer):
    son = serializers.SerializerMethodField()

    class Meta:
        model = models.Resource
        fields = '__all__'

    def get_son(self, obj):
        if obj.son.exists():
            ser = self.__class__(obj.son.all(), many=True)
            return ser.data
        return None
